package cn.tedu._05mvcboot01.controller;

import cn.tedu._05mvcboot01.pojo.dto.UserLoginDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Controller注解：将此类标识为控制器类,用于接收客户端发送的请求;
 */
@Controller
public class UserController {
    /**
     * 1.RequestMapping注解：根据请求的URL地址,映射到对应的控制器方法;
     * 2.ResponseBody注解：允许控制器方法以返回值的形式返回给客户端数据;
     */
    @RequestMapping("/v1/users/login")
    @ResponseBody
    public String login(){
        return "登录成功";
    }

    /**
     * 注册功能
     * 1.GET请求：http://localhost:8080/v1/users/reg?username=tedu&password=123456
     * 2.POST请求：
     *   <form action="地址" method="post">
     *      <input type="text" name="username">
     *      <input type="text" name="password">
     *      <input type="submit" value="注册">
     *   </form>
     */
    @RequestMapping("/v1/users/reg")
    @ResponseBody
    public String reg(){
        return "注册成功";
    }

    /**
     * 1.接收数据方式1:通过HttpServletRequest接收;
     *  - http://localhost:8080/v1/users/login1?username=xxx&password=yyy
     */
    @RequestMapping("/v1/users/login1")
    @ResponseBody
    public String login1(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        return username + "-" + password;
    }

    /**
     * 2.接收数据方式2:通过DTO类方式接收;
     *   - http://localhost:8080/v1/users/login2?username=xxx&password=yyy
     */
    @RequestMapping("/v1/users/login2")
    @ResponseBody
    public String login2(UserLoginDTO userLoginDTO){
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();

        return username + "-----" + password;
    }

    /**
     * 3.接收数据方式3：通过声明参数的方式接收;
     *   - http://localhost:8080/v1/users/login3?username=xxx&password=yyy
     */
    @RequestMapping("/v1/users/login3")
    @ResponseBody
    public String login3(String username, String password){
        return username + "++++++" + password;
    }
}









